package com.zhou.bjgl.bjgl.business_web.controller;


import com.zhou.bjgl.bjgl.business_web.service.CommonService;
import com.zhou.bjgl.bjgl.exception.BwException;
import com.zhou.bjgl.bjgl.exception.BwExceptionEnum;
import com.zhou.bjgl.bjgl.myAnnotations.UserLoginToken;
import com.zhou.bjgl.bjgl.req.b_teacher.BTeacherQueryReq;
import com.zhou.bjgl.bjgl.req.user.UserReq;
import com.zhou.bjgl.bjgl.resp.teacher.BTeacherResp;
import com.zhou.bjgl.bjgl.resp.PageResp;
import com.zhou.bjgl.bjgl.resp.common.ApiRestResponse;
import com.zhou.bjgl.bjgl.business_web.service.BTeacherService;
import com.zhou.bjgl.bjgl.resp.user.BUserForInfoResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  教师管理模块
 * </p>
 *
 * @author jz
 * @since 2021-07-04
 */
@RestController
@RequestMapping("/bjgl/b-teacher")
@Api("老师管理")
public class BTeacherController {

    @Resource
    private BTeacherService bTeacherService;

    @Resource
    private CommonService commonService;

    @ApiOperation("添加老师")
    @PostMapping("/add")
    @UserLoginToken
    public ApiRestResponse add(BTeacherQueryReq req){

        //查询是否有相同用户名
        UserReq userReq = new UserReq();
        userReq.setUsername(req.getUsername());
        BUserForInfoResp userInfo = commonService.getUserInfoForEdit(userReq);
        if (userInfo != null) {
            if ( "edit".equals(req.getTypeForEdit()) && !ObjectUtils.nullSafeEquals(req.getId(), userInfo.getId())) {
                throw new BwException(BwExceptionEnum.NAME_EXISTED);
            }
            if ("add".equals(req.getTypeForEdit())){
                throw new BwException(BwExceptionEnum.NAME_EXISTED);
            }

        }

        bTeacherService.add(req);
        return ApiRestResponse.success();
    }

    @ApiOperation("查询老师列表")
    @GetMapping("/query")
    @UserLoginToken
    public ApiRestResponse query(BTeacherQueryReq req) {
        PageResp<BTeacherResp> bTeacherRespPageResp = bTeacherService.queryTea(req);
        return ApiRestResponse.success(bTeacherRespPageResp);
    }

    @ApiOperation("删除老师")
    @PostMapping("/delete")
    @UserLoginToken
    public ApiRestResponse delete(@RequestBody BTeacherQueryReq req) {
        bTeacherService.deleteTea(req);
        return ApiRestResponse.success();
    }
}

